<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <p>封装cookie 目的是给cookie加上增值、修改、删除、获取对应的函数</p>
    <script>
        // console.log(document.cookie.prototype);
        function Cookie() {

        }

        // 新增和修改
        Cookie.prototype.setCooookie = function (name, val) {
            document.cookie = name + '=' + val;
        }
        // 获取
        Cookie.prototype.getVal151 = function (name) {
            var arry = document.cookie.split('; '); // 根据;分割成数组
            for (var i = 0; i < arry.length; i++) {
                var n_arry = arry[i].split('=');
                if (n_arry[0] == name) {
                    return n_arry[1]; // 返回对应的数据 而不是一个完整的对象
                }
            }
        }
        // 删除
        Cookie.prototype.delItem = function(name) {
            // 将对应的一项改为null   正确的写法是设置为超时
            document.cookie = name + '=' + null;
        }
    </script>
</body>

</html>